home *** CD-ROM | disk | FTP | other *** search
- #include "stdafx.h"
-
- char key[256];
- DWORD key_up[3], key_down[3], key_left[3], key_right[3], key_fire[3];
-
- static LPDIRECTINPUTDEVICE2 keyboard = 0;
- static int key_last[256];
- static int key_double[256];
-
- static void reset_keyboard()
- {
- for (int i = 0; i < 256; i++)
- {
- key[i] = 0;
- key_last[i] = 0;
- key_double[i] = FALSE;
- }
-
- }
-
- void init_keyboard()
- {
- keyboard = create_input_device_keyboard();
-
- reset_keyboard();
- }
-
- void deinit_keyboard()
- {
- if (keyboard != 0)
- {
- keyboard->Unacquire();
-
- keyboard->Release();
- }
- }
-
- LPDIRECTINPUTDEVICE2 create_input_device_keyboard()
- {
- LPDIRECTINPUTDEVICE2 keyboard;
-
- keyboard = create_input_device(GUID_SysKeyboard);
-
- if (FAILED(keyboard->SetCooperativeLevel(mainwindowhandle, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND)))
- error("Unable to set cooperative level for keyboard");
-
- if (FAILED(keyboard->SetDataFormat(&c_dfDIKeyboard)))
- error("Unable to set data format to keyboard");
-
- DIPROPDWORD dipdw;
-
- dipdw.diph.dwSize = sizeof(DIPROPDWORD);
- dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
- dipdw.diph.dwObj = 0;
- dipdw.diph.dwHow = DIPH_DEVICE;
- dipdw.dwData = KEY_BUFFERSIZE;
-
- if (FAILED(keyboard->SetProperty(DIPROP_BUFFERSIZE, &dipdw.diph)))
- error("Unable to set keyboard buffer size");
-
- return keyboard;
- }
-
- void readout_keyboard()
- {
- // Get the state of the keyboard
-
- if (FAILED(keyboard->GetDeviceState(sizeof(key), key)))
- {
- keyboard->Acquire();
-
- if (FAILED(keyboard->GetDeviceState(sizeof(key), key)))
- {
- reset_keyboard();
- return;
- }
- }
-
- // Get the state in a buffer for checking doubleclicks
-
- DIDEVICEOBJECTDATA dod[KEY_BUFFERSIZE];
- DWORD items = KEY_BUFFERSIZE;
-
- if (FAILED(keyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), dod, &items, 0)))
- {
- keyboard->Acquire();
-
- if (FAILED(keyboard->GetDeviceData(sizeof(DIDEVICEOBJECTDATA), dod, &items, 0)))
- {
- reset_keyboard();
- return;
- }
- }
-
- for (DWORD d = 0; d < items; d++)
- {
- // Check if this means a button is pressed
-
- if (dod[d].dwData & 0x80)
- {
-
- if (dod[d].dwTimeStamp - key_last[dod[d].dwOfs] <= KEY_DCLICKTIME)
- {
- // This is a double click
-
- key_last[dod[d].dwOfs] = 0;
- key_double[dod[d].dwOfs] = TRUE;
- }
- else
- {
- // It's not a double click, but remember last time this key was pressed
-
- key_last[dod[d].dwOfs] = dod[d].dwTimeStamp;
- }
- }
- }
- }
-
- cKeyboard::cKeyboard(int num)
- {
- up = key_up[num];
- down = key_down[num];
- left = key_left[num];
- right = key_right[num];
- fire = key_fire[num];
-
- reset();
- }
-
- void cKeyboard::reset()
- {
- key_last[up] = 0;
- key_double[up] = FALSE;
- }
-
- #define pressed(x) (key[x] & 0x80)
-
- void cKeyboard::determine(cPlayer *p)
- {
- // Make sure structures are up to date
-
- readout_keyboard();
-
- // Cannot do anything when not active
-
- if (!p->is_active())
- {
- reset();
- return;
- }
-
- // Check fire button
-
- if (pressed(fire))
- {
- if (pressed(up))
- p->fire_up();
- else if (pressed(down) && (pressed(left) || pressed(right)))
- p->fire_down();
- else
- p->fire();
- }
-
- // Cannot do anything more when captured
-
- if (p->is_captured())
- {
- reset();
-
- return;
- }
-
- // Jetpack on double click up
-
- if (key_double[up])
- {
- p->jet_on();
-
- key_double[up] = FALSE;
- }
-
- // Do movement
-
- if (p->is_walking())
- {
- if (pressed(left))
- p->walk_left();
- else if (pressed(right))
- p->walk_right();
- else
- p->walk_h_halt();
-
- if (pressed(up) && !pressed(fire))
- p->jump();
- else if (pressed(down))
- p->duck();
- }
- else if (p->is_ducked())
- {
- if (!pressed(down))
- p->stand();
-
- if (pressed(left))
- p->duck_left();
- else if (pressed(right))
- p->duck_right();
- }
- else if (p->is_jumping())
- {
- if (pressed(left))
- p->jump_left();
- else if (pressed(right))
- p->jump_right();
- else
- p->jump_h_halt();
- }
- else if (p->is_jetting())
- {
- if (pressed(up))
- p->jet_up();
- else if (pressed(down))
- p->jet_off();
- else
- p->jet_v_halt();
-
- if (pressed(left))
- p->jet_left();
- else if (pressed(right))
- p->jet_right();
- else
- p->jet_h_halt();
- }
- else if (p->is_climbing())
- {
- if (pressed(up))
- p->climb_up();
- else if (pressed(down))
- p->climb_down();
- else
- p->climb_v_halt();
-
- if (pressed(left))
- p->climb_left();
- else if (pressed(right))
- p->climb_right();
- else
- p->climb_h_halt();
- }
- }
-